#ifndef __MYSQLCONNPOOL_H__
#define __MYSQLCONNPOOL_H__

#include <condition_variable>
#include <mutex>
#include <queue>
#include "MysqlConn.h"
#include <thread>
#include <memory>

using std::shared_ptr;
using std::thread;
using std::queue;
using std::mutex;
using std::condition_variable;
using std::unique_lock;
using std::lock_guard;

class MysqlConnPool
{
public:
    static MysqlConnPool *getMysqlConnPool();
    MysqlConnPool(const MysqlConnPool &obj) = delete;
    MysqlConnPool &operator=(const MysqlConnPool &obj) = delete;
    shared_ptr<MysqlConn> getConnection();
    void exit();

    ~MysqlConnPool();

private:
    MysqlConnPool();
    void produceConnection();
    void recycleConnection();
    
    void addConnection();

    bool _isRunning;
    string _ip;
    string _user;
    string _passwd;
    string _dbName;
    unsigned short _port;
    int _minSize;
    int _maxSize;
    int _curSize;
    int _timeout;
    int _maxIdleTime;
    queue<MysqlConn*> _connections;
    mutex _mtx;
    condition_variable _condProduce;
    condition_variable _condConsume;
};

#endif      